<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of index
 *
 * @author bancan
 */
class User extends Controller {

    public function __construct() {
        parent::__construct();

//        echo 'dashboard ctrl';

        Session::init();

        $loggeg = Session::get('loggedIn');
        $role = Session::get('role');

        if ($loggeg == false || $role != 'Admin') {
            Session::Destroy();
            header('location:' . URL . 'error');
            exit;
        }
//        print_r($_SESSION['role']);
    }

    public function index() {
//        $item = 10;
//
//        $start = ($x - 1) * 10;
        //merefer user model user fungsi userlist
        $this->view->userList = $this->model->userList();

        //pengaturan jumlah page yang akan ditampilkan
        $this->view->page = $this->model->page();

        $this->view->render('user/index');
    }

    public function userForm() {
        $this->view->render('user/add_user');
    }

    public function addSave() {

        $data = array();

        $data['username'] = $_POST['username'];
        $data['password'] = $_POST['password'];
        $data['role'] = $_POST['role'];

        $this->model->addUser($data);
        header('location:' . URL . 'user/index/1');
    }

    public function delete($id) {
        $this->model->deleteUser($id);
        header('location:' . URL . 'user/index/1');
    }

    public function edit($id) {

        Session::set('edit', $id);
        $this->view->user = $this->model->editUser($id);
        $this->view->render('user/edit_user');
    }

    public function editSave() {
        $id = Session::get('edit');
        
        $data['id'] = $id;
        $data['username'] = $_POST['username'];
        $data['password'] = $_POST['password'];
        $data['role'] = $_POST['role'];
//        print_r($array);
        unset($_SESSION['edit']);
        $this->model->editSave($data);

        header('location:' . URL . 'user');
    }

    public function lagi() {
        $this->view->render('user/lagi');
    }

    public function user_detail() {

        $this->view->user_detail = $this->model->user_detail();
        $this->view->render('user/user_detail');
    }

    public function detail_coba() {
        $this->view->detail_coba = $this->model->detail_coba();
        $this->view->render('user/detail_coba');
//        print_r($data2);
        
    }

}

?>
